<template>
  <div>
    <el-card class="box-card" style="height:800px">
      <span slot="header">
        <navigation />
      </span>
      <el-scrollbar class="page-component__scroll" style="height:100%">
        <div style="height:700px;">
          <el-table
            :header-cell-style="{ background: '#f5f5f5' }"
            class="perTabSty"
            :data="tableData"
            style="width: 100%;margin-bottom: 20px;"
            row-key="id"
            border
            height="680px"
            default-expand-all
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          >
            <el-table-column
              label="序号"
              width="100"
              prop="index"
              align="center"
            ></el-table-column>
            <el-table-column prop="name" label="重点工作名称"></el-table-column>
            <!-- <el-table-column prop="number" label="全年目标任务量"></el-table-column> -->
            <el-table-column
              align="center"
              prop="departmentName"
              label="领导小组"
            ></el-table-column>

            <el-table-column label="操作时间" width="180">
              <template slot-scope="scope" v-if="scope.row.tscAuditorName">{{
                timestampToTimes(scope.row.updateTime)
              }}</template>
            </el-table-column>
            <!-- <el-table-column label="状态" width="200">
          <template slot-scope="scope">
            {{ dataType(scope.row) }}
          </template>
            </el-table-column>-->
            <el-table-column
              align="center"
              prop="tscAuditorName"
              label="申请人"
              width="100"
            ></el-table-column>
            <el-table-column label="操作" align="center" width="250">
              <template
                slot-scope="scope"
                v-if="!scope.row.children || scope.row.children.length == 0"
              >
                <el-button type="text" @click="handleLook(scope.row)"
                  >查看</el-button
                >
                <el-button type="text" @click="handFileList(scope.row)"
                  >附件列表</el-button
                >
                <el-button type="text" @click="dataGrade(scope.row)"
                  >通过</el-button
                >
                <el-button type="text" @click="regect(scope.row)"
                  >驳回</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-scrollbar>
      <!-- 附件列表弹框 -->
      <el-dialog
        width="60%"
        title="附件列表配置"
        :visible.sync="dialogFormVisible"
        :close-on-click-modal="false"
        @closed="closeDialog"
      >
        <el-form
          :inline="true"
          label-width="95px"
          :model="dutyFrom"
          :hide-required-asterisk="false"
        >
          <!-- <el-form-item label="年份：" prop="year">
            <el-date-picker
              class="userIntSize"
              v-model="dutyFrom.year"
              type="year"
              placeholder="选择年"
              @change="changeYear"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="季度：" prop="quarter">
            <el-select class="userIntSize" v-model="dutyFrom.quarter" clearable>
              <el-option
                v-for="(item, index) in quarterList"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>-->
          <!-- <el-form-item>
            <el-button
              size="small"
              class="saveBtn"
              type="primary"
              @click="submitForm"
              >添加</el-button
            >
          </el-form-item>-->
        </el-form>
        <el-table :data="fileTableData" border style="width: 100%">
          <el-table-column prop="file" label="附件">
            <template slot-scope="scope">
              <a :href="returnFile(scope.row.fileName)">
                {{ scope.row.fileName }}
              </a>
            </template>
          </el-table-column>
          <el-table-column prop="addTime" label="上传时间"></el-table-column>
          <el-table-column prop="userName" label="上传人"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button @click="removeFile(scope.row)" type="text"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
      <el-dialog
        title="选择季度"
        :visible.sync="showAddFormdialog"
        width="400px"
        @close="cancelForm('taskRef')"
      >
        <el-form ref="taskRef" :model="taskForm" label-width="80px">
          <el-form-item label="季度:" prop="quarter">
            <el-select
              class="userIntSize"
              v-model="taskForm.quarter"
              clearable
              @change="quarterChange"
              placeholder="请选择"
            >
              <el-option
                v-for="item in quarterList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="驳回原因:" prop="regectMsg" v-if="!btnStatus">
            <el-input
              class="userIntSize"
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="taskForm.regectMsg"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              style="width:70%"
              type="primary"
              @click="submitForms('taskRef')"
              >确认</el-button
            >
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import configJson from "../../api/config";
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      dutyFrom: {
        year: "",
        quarter: "",
        file: "",
        fileType: ""
      },
      taskFlag: "",
      quarterList: [
        { id: 1, name: "第一季度" },
        { id: 2, name: "第二季度" },
        { id: 3, name: "第三季度" },
        { id: 4, name: "第四季度" }
      ],
      fileOption: [],
      // 选择附件
      // uploadUrl: configJson.API_URL + "fileController/uploadFile",
      // fileName: "",
      // fileUrlEnd: "",
      // fileList: [],
      fileTableData: [],
      fileTypeList: [],
      fileStrList: [],
      showAddFormdialog: false,
      taskForm: {
        departmentId: "",
        type: 2,
        quarter: "",
        id: "",
        status: "",
        regectMsg: ""
      },
      btnStatus: true,
      taskIdData: [],
      arrData: [],
      arrData1: []
    };
  },
  mounted() {
    this.getTaskList();
    this.getFileList();
    this.getFileType();
  },
  methods: {
    // 时间转换
    timestampToTimes(timestamp) {
      var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear();
      var M =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      var D = date.getDate() + " ";
      var h = date.getHours() + ":";
      var m = date.getMinutes() + ":";
      var s = date.getSeconds();
      // return Y + M + D;
      let _time = `${Y}年${M}月${D}日`;
      return _time;
    },
    returnFile(val) {
      return `${configJson.API_URL}/fileController/${val}`;
    },
    transLate(val) {
      let text = "";
      switch (val) {
        case 1:
          text = "第一季度";
          break;
        case 2:
          text = "第二季度";
          break;
        case 3:
          text = "第三季度";
          break;
        default:
          text = "第四季度";
      }
      return text;
    },
    quarterChange(val) {
      let str = {
        taskId: `${this.taskIdData.id}`,
        departmentTypeId: null,
        time: val
      };
      // this.$axios
      //   .post("deptTask/getDeptList", str)
      //   .then(res => {
      //     if (res.code == 200) {
      //       console.log(res, 8888888);
      //       this.arrData1 = [];
      //       this.arrData = [];
      //       if (res.data.length == 0) {
      //         this.arrData1 = [];
      //         this.arrData = [];
      //       } else {
      //         res.data.forEach(i => {
      //           if (val == 1) {
      //             this.arrData.push(i.sc1Id);
      //             this.arrData1.push(i.sc1Id);
      //           } else if (val == 2) {
      //             this.arrData.push(i.sc2Id);
      //             this.arrData1.push(i.sc2Id);
      //           } else if (val == 3) {
      //             this.arrData.push(i.sc3Id);
      //             this.arrData1.push(i.sc3Id);
      //           } else if (val == 4) {
      //             this.arrData.push(i.sc4Id);
      //             this.arrData1.push(i.sc4Id);
      //           }
      //         });
      //       }
      //       this.setstauas();
      //     }
      //   })
      //   .catch(err => {
      //     console.log(err);
      //   });
      this.$axios
        .post("deptTask/getDeptQuarterExamine", str)
        .then(res => {
          if (res.code == 200) {
            console.log(res);
            // res.data.forEach(i => {
            //   if (val == 1) {
            //     this.arrData.push(i.sc1Id);
            //     this.arrData1.push(i.sc1Id);
            //   } else if (val == 2) {
            //     this.arrData.push(i.sc2Id);
            //     this.arrData1.push(i.sc2Id);
            //   } else if (val == 3) {
            //     this.arrData.push(i.sc3Id);
            //     this.arrData1.push(i.sc3Id);
            //   } else if (val == 4) {
            //     this.arrData.push(i.sc4Id);
            //     this.arrData1.push(i.sc4Id);
            //   }
            // });
            this.arrData = res.data;
            this.quarterChangeTow(val);
            console.log(this.arrData);
            // this.setstauas();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    quarterChangeTow(val) {
      let str = {
        taskId: `${this.taskIdData.id}`,
        departmentTypeId: null,
        time: val
      };
      this.$axios
        .post("/deptTask/getDeptQuarterIds", str)
        .then(res => {
          if (res.code == 200) {
            console.log(res);

            // res.data.forEach(i => {
            //   if (val == 1) {
            //     this.arrData.push(i.sc1Id);
            //     this.arrData1.push(i.sc1Id);
            //   } else if (val == 2) {
            //     this.arrData.push(i.sc2Id);
            //     this.arrData1.push(i.sc2Id);
            //   } else if (val == 3) {
            //     this.arrData.push(i.sc3Id);
            //     this.arrData1.push(i.sc3Id);
            //   } else if (val == 4) {
            //     this.arrData.push(i.sc4Id);
            //     this.arrData1.push(i.sc4Id);
            //   }
            // });
            this.arrData = res.data;
            this.arrData1 = res.data;
            this.setstauas();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    setstauas() {
      if (this.arrData.length == 0) {
        this.$message.error("当前选项不能申请审核，请先进行打分操作");
        return;
      }
      let obj = {
        id: this.arrData[0],
        type: 2,
        time: this.taskForm.quarter
      };
      this.$axios
        .post("deptScore/load", obj)
        .then(res => {
          if (res.code == 200) {
            if (res.data) {
              this.taskForm.status = res.data.status;
            } else {
              this.taskForm.id = "";
              this.taskForm.status = "";
            }
          } else {
            this.$message.error(`${res.msg}`);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    dataGrade(row) {
      this.taskIdData = row;
      this.showAddFormdialog = true;
      this.taskForm.departmentId = row.departmentId;
      this.btnStatus = true;
    },
    regect(row) {
      console.log(row);
      this.taskIdData.id = row.id;
      this.showAddFormdialog = true;
      this.taskForm.departmentId = row.departmentId;
      this.btnStatus = false;
    },
    getFileType() {
      this.$axios
        .post("fileLibTypeController/getList")
        .then(res => {
          if (res.code == 200) {
            this.fileTypeList = res.data;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    cancelForm() {
      this.taskForm = {
        departmentId: "",
        type: 2,
        quarter: "",
        id: "",
        status: "",
        regectMsg: ""
      };
    },
    fileLists(arr) {
      let str = arr.split("/");
      return str.pop();
    },
    // 获取任务列表
    getTaskList() {
      this.$axios
        .post("task/getList")
        .then(res => {
          if (res.code == 200) {
            this.tableData = res.data;
            let _taskId = localStorage.getItem("taskId");
            this.tableData.forEach((item, index) => {
              if (_taskId == "null" || _taskId == "undefined") {
                item.isDisplay = true;
                if (item.children || scope.row.children.length != 0) {
                  item.children.forEach((i, index2) => {
                    i.isDisplay = true;
                    i.index = `${index + 1}.${index2 + 1}`;
                  });
                }
                item.index = index + 1;
              } else {
                item.index = index + 1;
                if (item.id == _taskId) {
                  item.isDisplay = true;
                } else {
                  item.isDisplay = false;
                }
              }
            });
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 查看
    handleLook(row) {
      console.log(row, "row");
      this.$router.push({
        path: "/tenTaskScoreAuditPage",
        query: { id: row.id, name: row.name }
      });
    },
    // 打开附件列表弹框
    handFileList(row) {
      this.dialogFormVisible = true;
      this.taskFlag = row.id;
      this.getTabData();
    },
    getTabData() {
      this.$axios
        .post("/taskFile/getList", { taskId: this.taskFlag, type: 2 }, 2)
        .then(res => {
          if (res.code == 200) {
            this.fileTableData = res.data;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 选择年
    changeYear(val) {
      this.dutyFrom.year = val.toLocaleDateString();
    },
    // 获取附件列表
    getFileList() {
      this.$axios
        .post("fileController/getList")
        .then(res => {
          if (res.code == 200) {
            this.fileOption = res.data;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    submitForm() {
      if (this.dutyFrom.year == "") {
        this.$message.error("请选择年份");
      } else if (this.dutyFrom.quarter == "") {
        this.$message.error("请选择季度");
      } else if (this.file == "") {
        this.$message.error("请选择附件");
      } else {
        let obj = {
          taskId: this.taskFlag,
          year: this.dutyFrom.year.substring(0, 4),
          quarter: this.dutyFrom.quarter,
          file: this.dutyFrom.file
        };
        this.$axios
          .post("taskScoreFile/add", obj, 2)
          .then(res => {
            if (res.code == 200) {
              this.$message({
                message: "添加成功",
                type: "success"
              });
              this.dutyFrom.year = "";
              this.dutyFrom.quarter = "";
              this.fileList = [];
              this.getTabData();
            }
          })
          .catch(err => {
            console.log(err);
          });
      }
    },
    submitForms() {
      if (this.arrData == false) {
        this.$message({
          message: "当前选项不能申请审核，请先进行打分操作",
          type: "error"
        });
        return;
      }
      if (!this.taskForm.quarter) {
        this.$message.error("请先选择季度");
        return;
      }
      let obj = {
        taskId: this.taskIdData.id,
        ids: this.arrData1,
        quarter: this.taskForm.quarter
      };
      if (this.btnStatus) {
        if (this.taskForm.status == 1) {
          obj.status = 1;
          this.getStatus(obj);
        } else if (this.taskForm.status == 2) {
          this.$message.error("审核通过状态,不能再通过");
        } else if (this.taskForm.status == -1) {
          this.$message.error("该状态未提交申请,不能点击通过");
        } else {
          this.$message.error("通过状态,不能再通过");
        }
      } else {
        if (this.taskForm.status == 2 || this.taskForm.status == 1) {
          obj.status = -1;
          obj.message = this.taskForm.regectMsg;
          this.getStatus(obj);
        } else if (this.taskForm.status == -1) {
          this.$message.error("该状态处于驳回状态,不能驳回");
        } else {
          this.$message.error("此状态,不能驳回");
        }
      }
      setTimeout(() => {
        this.showAddFormdialog = false;
      }, 500);
    },
    getStatus(obj) {
      this.$axios
        .post("departmentScoreCheckLog/update", obj, 2)
        .then(res => {
          if (res.code == 200) {
            if (res.data) {
              if (this.btnStatus) {
                this.$message({
                  message: "审核通过",
                  type: "success"
                });
              } else {
                this.$message({
                  message: "驳回成功",
                  type: "success"
                });
              }
              this.getFileList();
              this.showAddFormdialog = false;
            }
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    removeFile(row) {
      this.$confirm("确认删除？")
        .then(_ => {
          this.$axios
            .post("taskScoreFile/remove", { id: row.id }, 2)
            .then(res => {
              if (res.code == 200) {
                this.$message({
                  message: "删除成功",
                  type: "success"
                });
                this.getTabData();
              }
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(_ => {});
    },
    closeDialog() {
      this.dutyFrom = {
        year: "",
        quarter: "",
        file: "",
        fileType: ""
      };
    },
    fileChange(val) {
      this.$axios
        .post("fileController/getList", { type: val }, 2)
        .then(res => {
          if (res.code == 200) {
            this.fileStrList = res.data;
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>
<style scoped>
.saveBtn {
  margin-left: 50px;
}
</style>
